from pkg.voice import out
from pkg.gui import NewUserClass
from pkg.gui import ChooseUserClass
from pkg.db import db


def build_user(times=0):
    out.output_thread('请输入新建用户信息')
    new_user_dlg = NewUserClass.NewUserClass()
    if new_user_dlg.exec()==1:
        data=(new_user_dlg.id_input.text(),new_user_dlg.nick_input.text(),new_user_dlg.sex_comb.currentIndex(),
              new_user_dlg.birth_date.selectedDate().toString('yyyy-MM-dd'))
        if db.add_user(data):
            db.build_user_msg_tab()
            out.output('用户新建成功')
            return True
        else:
            if(times==3):
                out.output('用户新建失败')
                return
            out.output('用户新建失败，请再试一次')
            return build_user(times+1)
    else:
        out.output('您放弃了用户创建')
        return False


def choose_user(users):
    choose_user_dlg=ChooseUserClass.ChooseUserClass(users)
    if choose_user_dlg.exec()==1:
        return choose_user_dlg.user_list.currentText()
    else:
        return None
